#include "render_globals.h"
#include "render_object.h"
#include "render.h"


fonts           fnts;

SDL_Surface*    screen;

bool fullscreen = false;

html_parser     parser;
html_document   doc;


char base_path[] = { "../html_parser/tests/" };

//helper .. move to generic file -- TODO
char* make_path(const char*);
char* make_path(const char* path_rel)
{
#ifdef _WII_
	static char abs_path[100] = {0};
	sprintf(abs_path,"sd:/apps/wiibrowse/%s",path_rel);

	return abs_path;
#else
	return (char*)path_rel;
#endif

}

void render_test()
{

    char* buffer = 0;
    FILE* f = 0;
    char path[255] = {0};
    sprintf(path,"%stest2.html",base_path);
    f = fopen(path,"r");

    if (!f) return;

    fseek(f,0,SEEK_END);
    unsigned int size = ftell(f);
    fseek(f,0,SEEK_SET);

    buffer = new char[size+1];
    memset(buffer,0,size+1);

    fread(buffer,size,1,f);

    fclose(f);

    parser.parse_go(&doc,buffer); // pass in the html_document object and the buffer

    delete [] buffer;

}


int main(int argc, char **argv)
{

    if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER  ) < 0 ) {
        exit(1);
    }

	if ( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, BITDEPTH,
			 (fullscreen ? SDL_FULLSCREEN : 0) | SDL_HWSURFACE | SDL_DOUBLEBUF ) < 0 ) { //SDL_HWSURFACE
		exit(1);
	}

	SDL_ShowCursor(1);

	screen = NULL;
	screen = SDL_GetVideoSurface();

	if (!screen) exit(1);


    render_test();

    render_engine   render(base_path,&doc,SCREEN_WIDTH);

    bool running = true;
    SDL_Event event;
    int scroll_value = 0;
    int last_y = -1;
    render.render_start(); // calculate everything
    //render.render_set(&event);
    while(running)
    {
        // move page up / down

        render.view_y += scroll_value;

        if (render.view_y > 0) render.view_y = 0;
        if (render.page_height > SCREEN_HEIGHT)
            if (render.view_y < -(render.page_height-SCREEN_HEIGHT)) render.view_y = -(render.page_height-SCREEN_HEIGHT);



        while (SDL_PollEvent(&event)) {
            switch(event.type)
            {
                case SDL_QUIT:
                  running = false;
                  break;

                case SDL_KEYDOWN:
                {
                    SDLKey key = event.key.keysym.sym;

                    switch(key)
                      {
                      case SDLK_ESCAPE:
                        running = false;
                        break;
                      case SDLK_UP:
                        scroll_value = 8;
                        break;
                       case SDLK_DOWN:
                        scroll_value = -8;
                        break;

                        default:
                        break;
                      }
                  }
                  break;
                case SDL_KEYUP:      /* A keyrelease! */
                {
                    SDLKey key = event.key.keysym.sym;
                    switch(key) {
                        case SDLK_DOWN:
                        case SDLK_UP:
                            scroll_value = 0;
                        break;

                        default:
                        break;
                    }

                }
                break;
            }
        }

        // render the page
        //if(last_y != render.view_y)
        {
            render.render_set(&event);
            last_y = render.view_y;
        }
        // flip
        SDL_Flip(screen);
        SDL_Delay(5);
    }

    SDL_Quit();

	return 0;

}
